package tx.bj12320.dao;

import java.sql.SQLException;
import java.util.*;

import javacommon.base.*;
import javacommon.util.*;
import javacommon.xsqlbuilder.XsqlBuilder;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import tx.bj12320.model.*;
import tx.bj12320.dao.*;
import tx.bj12320.service.*;
import tx.bj12320.vo.query.*;


import static cn.org.rapid_framework.util.ObjectUtils.*;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

@Repository
public class SmsInfoDao extends BaseHibernateDao<SmsInfo,Long>{

	public Class getEntityClass() {
		return SmsInfo.class;
	}

	public Page findPage(SmsInfoQuery query) {
        // [column]为字符串拼接, {column}为使用占位符. [column]为使用字符串拼接,如username='[username]',偷懒时可以使用字符串拼接
        // [column] 为PageRequest的属性
		String sql = "select t from SmsInfo t where 1=1 "
			  	+ "/~ and t.userInfoId = {userInfoId} ~/"
			  	+ "/~ and t.sellPhone = {sellPhone} ~/"
			  	+ "/~ and t.infoContent = {infoContent} ~/"
				+ "/~ and t.sendDatetime >= {sendDatetimeBegin} ~/"
				+ "/~ and t.sendDatetime <= {sendDatetimeEnd} ~/"
			  	+ "/~ and t.isGlobal = {isGlobal} ~/"
			  	+ "/~ and t.isSent = {isSent} ~/"
				+ "/~ order by [sortColumns] ~/";


		return pageQuery(sql,query);
	}

	public List<SmsInfo> findAllUnSendSms(final UserInfoQuery query) {
		return (List<SmsInfo>)getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				String sql = "select t from SmsInfo t where  t.isSent = 0 ";

				XsqlBuilder builder = getXsqlBuilder();
				//log.info(builder.generateHql(sql, new HashMap(),query).getXsql());
					return session.createQuery(builder.generateHql(sql,query).getXsql()).setProperties(query).list();
			}
		});
	}
}
